<?

class TaskController extends Controller{
    
    function actionIndex(){
        return $this->actionList();
    }
    
    function actionList(){
        
        $_PCODE = trim(substr(strip_tags($_GET['project']), 0, 60));
        $_TID = intval($_GET['tid']);
        
        $PROJECT = TaskProject::findByCode($_PCODE);
        if(!$PROJECT){
            throw new CException(TaskModule::t('message', 'Проект «'.$_PCODE.'» не найден.'));
        }else{
            
            if(!TaskUser::UserInProject($PROJECT->id)){
                throw new CHttpException(403,'Forbidden');
            }
            
            $this->pageTitle = TaskModule::t('app', 'Мои задачи');
            $this->breadcrumbs[TaskModule::t('app', 'Проекты')] = $this->createUrl('project/list');
            $this->breadcrumbs[] = $this->pageTitle;
            
            $NEWTASK = new FormNewTask;
            $NEWTASK->projectID = $PROJECT->id;
            if(isset($_POST['FormNewTask'])){
                $NEWTASK->attributes = $_POST['FormNewTask'];
                if($NEWTASK->validate()){
                    
                    $NEWTASK->files = $_FILES['FormNewTask'];
                    $res = $NEWTASK->createTask();
                    if(is_array($res)){
                        $err = array();
                        foreach($res as $k=>$e){
                            $err[] = implode('<br />', $e);
                        }
                        Yii::app()->user->setFlash(FL_ERROR, TaskModule::t('messages', 'Ошибка создания задачи.<br /><br />'.implode('<br />',$err)));
                    }elseif($res===true){
                        Yii::app()->user->setFlash(FL_SUCCESS, TaskModule::t('messages', 'Задача успешно создана.'));
                        $this->refresh();
                    }
                    
                }
            }
            
            $tali = array();
            $TASKSLINK = TaskResponsibleUser::model()->findAllByAttributes(array('user_id'=>Yii::app()->user->id));
            foreach($TASKSLINK as $TL){
                array_push($tali, $TL->task_id);
            }
            
            $TASKFILTER = new FormTaskListFilter;
            if(isset($_POST['FormTaskListFilter'])){
                $TASKFILTER->attributes = $_POST['FormTaskListFilter'];
                $TASKFILTER->save();
                $this->refresh();
            }
            $criteria = new CDbCriteria;
            $criteria->addColumnCondition(array('`t`.`project_id`'=>$PROJECT->id));
            if(Yii::app()->user->checkAccess('developer') || Yii::app()->user->checkAccess('client')){
                $criteria->addColumnCondition(array('`t`.`user_id`'=>Yii::app()->user->id));
                $criteria->addInCondition('`t`.`id`', $tali, 'OR');
            }
            if($_TID > 0) $criteria->addColumnCondition(array('`t`.`id`'=>$_TID));
            else{
                $criteria = $TASKFILTER->generate($criteria);
            }
            $TASKS_PROVIDER = new CActiveDataProvider(Task::model()->with('author','time','files','responsible','comments'), array(
                'criteria' => $criteria,
                'pagination'=>array( 'pageSize'=>50 ),
            ));
            
            $TASKS_INPROGRESS = array();
            $TIP = TaskTime::model()->findAllByAttributes(array(
                'user_id' => Yii::app()->user->id,
                'time_end' => 0
            ));
            foreach($TIP as $T){
                $TASKS_INPROGRESS[$T->task_id] = $T;
            }
            
            $this->render('list', array(
                '_TID' => $_TID,
                'PROJECT' => $PROJECT,
                'TASKFILTER' => $TASKFILTER,
                'TASKS_PROVIDER' => $TASKS_PROVIDER,
                'TASKS_INPROGRESS' => $TASKS_INPROGRESS,
                'NEWTASK' => $NEWTASK,
            ));
            
        }
        
    }
    
}
